package edu.xlh.travel.elasticsearch.utils;

import com.alibaba.fastjson.JSON;
import edu.xlh.travel.elasticsearch.common.EsConstant;
import edu.xlh.travel.pojo.Hotel;
import edu.xlh.travel.pojo.Scenic;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class BusinessQueryUtil<T> {
    @Autowired
    private ElasticsearchUtil<T> elasticsearchUtil;

    public List<Hotel> hotelQueryBuilders(Hotel hotel , Integer page, Integer limit) throws IOException {
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
                .must(QueryBuilders.termQuery("delFlag", "0"));

        if (StringUtils.isNotEmpty(hotel.getName())) {
            boolQueryBuilder.must(QueryBuilders.termQuery("name", hotel.getName()));
        }
        if (StringUtils.isNotEmpty(hotel.getAddr())) {
            boolQueryBuilder.must(QueryBuilders.termQuery("addr", hotel.getAddr()));
        }
        if (hotel.getPrice() != null) {
            boolQueryBuilder.must(QueryBuilders.rangeQuery("price").gte(hotel.getPrice() - 10).lte(hotel.getPrice() + 10));
        }
        if (hotel.getStock()!= null){
            boolQueryBuilder.must(QueryBuilders.rangeQuery("stock").gte(hotel.getStock() - 10).lte(hotel.getStock() + 10));
        }
        if (hotel.getBedNum()!= null){
            boolQueryBuilder.must(QueryBuilders.termQuery("bedNum",  hotel.getBedNum()));
        }
        List<Map<String, Object>> t_hotelMapList = elasticsearchUtil.QueryByCondition(EsConstant.E_HOTEL, boolQueryBuilder, page, limit);
        List<Hotel> list = new ArrayList<>();
        for (Map<String, Object> sourceAsMap : t_hotelMapList) {
            String jsonStringSON = JSON.toJSONString(sourceAsMap);
            Hotel hotelEntity = JSON.parseObject(jsonStringSON, Hotel.class);
            list.add(hotelEntity);
        }
        return list;
    }

    public List<Scenic> scenicQueryBuilders(Scenic scenic , Integer page, Integer limit) throws IOException {
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
                .must(QueryBuilders.termQuery("delFlag", "0"));
        if (StringUtils.isNotEmpty(scenic.getName())) {
            boolQueryBuilder.must(QueryBuilders.termQuery("name", scenic.getName()));
        }
        if (StringUtils.isNotEmpty(scenic.getContry())) {
            boolQueryBuilder.must(QueryBuilders.termQuery("contry", scenic.getContry()));
        }
        if (scenic.getPrice() != null) {
            boolQueryBuilder.must(QueryBuilders.rangeQuery("price").gte(scenic.getPrice() - 10).lte(scenic.getPrice() + 10));
        }
        if (scenic.getStock()!= null){
            boolQueryBuilder.must(QueryBuilders.rangeQuery("stock").gte(scenic.getStock() - 10).lte(scenic.getStock() + 10));
        }
        List<Map<String, Object>> t_scenicMapList = elasticsearchUtil.QueryByCondition(EsConstant.E_SCENIC, boolQueryBuilder, page, limit);
        List<Scenic> list = new ArrayList<>();
        for (Map<String, Object> sourceAsMap : t_scenicMapList) {
            String jsonStringSON = JSON.toJSONString(sourceAsMap);
            Scenic scenicEntity = JSON.parseObject(jsonStringSON, Scenic.class);
            list.add(scenicEntity);
        }
        return list;
    }
}
